<?php
/**
 * User: maoyao
 * DateTime: 2020/10/13 T 11:43
 * ProjectName:mozhe_com
 * Description:
 *
 */

namespace Hilaoyu\LaravelExtend\Services\Realnames;


use Hilaoyu\LaravelExtend\Entity;
use Hilaoyu\Utils\UtilHtml;
use Illuminate\Support\Carbon;


class RealnamesEntity extends Entity
{
    static $user_relname_cache = [];

    static protected function classNameModelMain(): string
    {
        return config('realname.records_model');
    }


    protected function alterDeleteBefore($model,$force=false)
    {
        //不允许删除
        return null;
    }

    public function useAsClient()
    {
        if ($client_config = config('realname.use_as_client')) {
            return new RealnameApiClient($client_config);
        }
        return false;
    }



    protected function alterSaveBefore($model, &$data)
    {
        $model = parent::alterSaveBefore($model, $data);
        //作为客户端使时
        if($client = $this->useAsClient()){
            $data = $client->save($model->attributesToArray());
            if(!$data || !($id = data_get($data,'id',''))){
                $this->setError($client->getError());
                return null;
            }

            $model = $this->model($id,true);
            $model->fill($data);

        }
        if('1' == $model->getAttribute('status')){
            if('1' != $model->getOriginal('status')){
                $model->fill(['check_time'=>Carbon::now()->toDateTimeString()]);
            }
        }else{
            $model->fill(['check_time'=>null]);
        }



        return $model;
    }



    public function buildStatusOptions($selected='',$add_title=''){


        $add_title = trim($add_title);
        $options = [
            ['value'=>'0','label'=>'待审核'],
            ['value'=>'1','label'=>'审核通过'],
            ['value'=>'2','label'=>'审核未通过'],
        ];

        if($add_title){
            array_unshift($options,['value'=>'','label'=>$add_title]);
        }

        return UtilHtml::formatHtmlOptions($options,'value','label','',$selected);
    }

    public function buildTypeOptions($selected='',$add_title=''){
        //作为客户端使用时
        if($client = $this->useAsClient()){
            $options = $client->cardTypeOptions($selected,$add_title);
            if(!$options){
                $this->setError($client->getError());
            }
            return $options;
        }

        $add_title = trim($add_title);
        $options = [];

        foreach ($this->model()::AllowedCardType() as $key => $name){
            $options[] = ['value'=>$key,'label'=>$name];
        }

        if($add_title){
            array_unshift($options,['value'=>'','label'=>$add_title]);
        }

        return UtilHtml::formatHtmlOptions($options,'value','label','',$selected);
    }


    public function fileUploaders()
    {
        if($client = $this->useAsClient()){
            return $client->fileUploaders();
        }
        return parent::fileUploaders();
    }

    public function findUserRealname($user_id){
        $user_id = trim($user_id);
        if(!$user_id){
            return null;
        }

        if(!array_key_exists($user_id,static::$user_relname_cache)){
            if($client = $this->useAsClient()){
                $model = $client->finUserRealname($user_id);
            }else{
                $model = $this->find(['user_id'=>$user_id]);
            }
            static::$user_relname_cache[$user_id] = $model;
        }


        return data_get(static::$user_relname_cache,$user_id);
    }


    /**
     * 检查用户实名认证是否已通过
     * @param $user_id
     * @return bool
     */
    public function userRealnameIsPassed($user_id){
        $model = $this->findUserRealname($user_id);
        return $model && $model->exists && '1' == $model->getAttribute('status');
    }

}
